//
//  BwtNativeAdManager.m
//  bwt_ad_flutter
//
//  Created by 帅 on 2025/6/24.
//

#import "BwtNativeAdManager.h"
#import "BwtNativeAd.h"
#import "BwtAdLogManager.h"
@implementation BwtNativeAdManager

+ (BwtNativeAdManager *)getBwtNativeAdManager {
    static BwtNativeAdManager *bwtNativeAdManager;
    if(!bwtNativeAdManager) {
        bwtNativeAdManager = [[BwtNativeAdManager alloc] init];
        [bwtNativeAdManager initData];
    }
    return bwtNativeAdManager;
}

- (void)initData{
    if (!_nativeAdMap) {
        _nativeAdMap = [[NSMutableDictionary alloc] initWithCapacity:0];
    }
}

- (void)loadNativeAd:(AdParams*)adParams isRequestIdfa:(bool)isRequestIdfa{
    BwtNativeAd *nativeAd = [[BwtNativeAd alloc] init];
    nativeAd.adParams = adParams;
    nativeAd.isRequestIdfa = isRequestIdfa;
    NSString *adId = adParams.adId;
    [_nativeAdMap setObject:nativeAd forKey:adId];
    [[BwtAdLogManager getBwtAdLogManager] log:@"开始信息流自渲染广告, adId: %@", adParams.adId];
    [nativeAd loadNativeAd];
}

- (UIView *)getNativeAdView:(NSString*)adId{
    if (adId) {
        BwtNativeAd *nativeAd = _nativeAdMap[adId];
        if (nativeAd) {
            return nativeAd.nativeAdView;
        }
    }
    return nil;
}

- (void)closeAd:(NSString *)adId{
    if (adId) {
        BwtNativeAd *nativeAd = _nativeAdMap[adId];
        if (nativeAd) {
            [nativeAd closeAd];
        }
    }
}

- (void)releaseAd:(NSString*)adId{
    if (adId) {
        BwtNativeAd *nativeAd = _nativeAdMap[adId];
        if (nativeAd) {
            [nativeAd releaseAd];
            [_nativeAdMap removeObjectForKey:adId];
            [[BwtAdLogManager getBwtAdLogManager] log:@"广告已释放, adId: %@", adId];
        }
    }
}

@end
